
let baseUrl = '';
let WebSocket_url=''
if (process.env.NODE_ENV == 'development') {
    baseUrl='http://120.55.77.106:3000'
} else {
	baseUrl='http://120.55.77.106:3000'
}
function request(url,method,data,header={}){

   
//判断档口是否审核成功
let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
  if(data){
	  // data.Cookie=uni.getStorageSync("Cookie")
	 
	  if(data.page){
		  if(data.page==0){
		  		  uni.showLoading({
		  		    title: '更新中...',
		  		  	mask:true
		  		  })
		  }
	  }else if(data.none_load){
		  delete data.none_load;//删除属性 
	  } else{
			  uni.showLoading({
				title: '更新中...',
				mask:true
			  })
	  }
	  
  }else{
	  uni.showLoading({
	    title: '更新中...',
	  	mask:true
	  })
	  var data={
		  // token:uni.getStorageSync("Cookie")
	  }
  }
  return new Promise((resolve,reject) => {
	var header={
		// 'content-type': 'application/x-www-form-urlencoded',
		'content-type': 'application/json',
		'authorization':uni.getStorageSync("Cookie"),
		'Cookie':'PHPSESSID='+uni.getStorageSync("Cookie"),
	}
	// #ifdef APP-PLUS||MP-WEIXIN||MP-ALIPAY||MP-KUAISHOU
	header['Referer']=baseUrl
	// #endif
    uni.request({
      url: baseUrl + url,
      method: method,
      data: data,
      header: header,
      success: function(resp) {
        setTimeout(()=>{
        	 uni.hideLoading()
         },500)
		if(typeof(resp)=='string'){
			resp=JSON.parse(resp)
		}
		
		
		if (resp.data.code == 200) {
			resolve(resp.data);
		} else {
			var message=''
			if(resp.data.error){
				 message=resp.data.error
			}
			if(message=='未提供令牌'){
				 message='请先登录'
			}
			console.log(resp.data.error,'222')
			if(url=='/api/getUserInfo'&&resp.data.error=='无效的令牌'){
				message='登录失效，请重新登录'
				uni.removeStorageSync('Cookie');
				uni.showToast({
					title: message,
					duration: 2000,
					icon: 'none'
				});
			}else if(url=='/api/qrScan'){
				resolve(resp.data);
			}else if(url=='/mapi/order/order_xq'&&resp.data.message=='找不到订单'){
				
			}else{
				uni.showToast({
					title: message,
					duration: 2000,
					icon: 'none'
				});
			}
			console.log(url)
			resolve(resp.data);
		}
      },
      fail: function(resp) {
        uni.hideLoading();
        reject(resp);
      },
	  complete(end) {
		  uni.stopPullDownRefresh();
	 }
    })
  })
}
 function  jspost(URL, PARAMS=[]) {
	 console.log(URL)
	 // #ifdef H5
      var temp = document.createElement("form");
      temp.action = URL;
      temp.method = "post";
      temp.target = "";//跳转到新页面
      temp.style.display = "none";
      for (var x in PARAMS) {
          var opt = document.createElement("textarea");
          opt.name = x;
          opt.value = PARAMS[x];
          temp.appendChild(opt);
      }
      document.body.appendChild(temp);
      temp.submit();
      return temp;
	  // #endif
  }


// 封装get方法
function get(obj) {
  return request(obj.url,'GET',obj.data)
}
// 封装post方法
function post(obj) {
  return request(obj.url,'POST',obj.data)
}

// 封装post方法
function put(obj) {
  return request(obj.url,'put',obj.data)
}
function delete_api(obj) {
  return request(obj.url,'delete',obj.data)
}

export default {
  request,
  get,
  post,
  put,
  delete_api,
  baseUrl,
  jspost,
  WebSocket_url
}
